# !/bin/bash

ew9500_video_dev="/dev/video_cz_sony"
ew9500_serial_dev="/dev/ttyCH9344USB0"

ew9500_init_status=`cat /home/cz/cz30t/init.ini`

version=`dpkg -l | grep c30n-bin | grep -Eo "*[0-9]+\.*[0-9]+\.*[0-9]+*[0-9]"`

if [ -e "$ew9500_video_dev" ]; #判断文件是否存在
then
	echo "$ew9500_video_dev is exist"

	if [ -e "$ew9500_serial_dev" ]; #判断串口设备是否存在
	then
		echo "$ew9500_serial_dev is exist"

		if [ $ew9500_init_status = 1 ]
		then
			echo "ew9500 is init ok, not restart init!"
		else
			echo "ew9500 is not init,now will to init!"
			if [ "${version:0:1}" == 1 ];
			then
				sudo systemctl stop cz30t
			elif [ "${version:0:1}" == 2 ];
			then
				sudo systemctl stop cz30t
				sudo systemctl stop camera_linux
			fi
			
			sudo /home/cz/cz30t/ew9500_params #执行初始化程序
			sudo echo 1 > /home/cz/cz30t/init.ini

			if [ "${version:0:1}" == 1 ];
                        then
                                sudo systemctl restart cz30t
                        elif [ "${version:0:1}" == 2 ];
                        then
                                sudo systemctl restart cz30t
                                sudo systemctl restart camera_linux
                        fi

			echo "ew9500 init ok!"
		fi
	else
		echo "$ew9500_serial_dev is not exist"
	fi
else
	echo "$ew9500_video_dev is not exist"
fi

